<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Data_fetcher extends CI_Model {
	
	
	function fetch_community_data($name)
	{								
		
		$data =  $this->db
						->get_where('tb_mas_community',array('strName'=>$name))
						->first_row();
						
		return $data;
						
	}
	
	function fetch_content_types()
	{								
		
		$data =  $this->db
						->get_where('tb_mas_content_types')
						->result_array();
						
		return $data;
						
	}
	
    function fetch_news($limit=20,$offset=0)
    {
        return $this->db
                    ->get_where('tb_mas_content',array("enumEnabled"=>"enabled","enumYahoo"=>"enabled"))
                    ->result_array();
    
    }
    
	function fetch_content_type_archive($typeName,$community,$limit=10,$offset=0,$id=0)
	{
		$type = current($this->db->get_where('tb_mas_content_types',Array('strName'=>$typeName))->result_array());
			if(isset($type['intID']))
			{
				$data = $this->db
                        ->where(Array('fk-tb_mas_content_types'=>$type['intID'],'fk-tb_mas_community'=>$community,'enumEnabled'=>'enabled','intID !='=>$id))
                        ->order_by('dteStart','desc')
                        ->limit($limit,$offset)
                        ->get('tb_mas_content')   
                        ->result_array();
				if(!isset($data[0]['intID']))
				{
					$data['error'] = 'No Content Found!';
				}
			}
			else
			{
			$data['error'] = 'Type Not Found!';
			}
		return $data;
	}
	
    function fetch_content_tags($tag,$community,$limit=10,$offset=0,$id=0)
	{
		
				$data = $this->db
                        ->where(Array('strTags regexp'=>$tag,'fk-tb_mas_community'=>$community,'enumEnabled'=>'enabled'))
                        ->order_by('dteStart','desc')
                        ->limit($limit,$offset)
                        ->get('tb_mas_content')   
                        ->result_array();
				
        if(!isset($data[0]['intID']))
        {
					$data['error'] = 'No Content Found!';
        }
		
		return $data;
	}
    
	function fetch_content($type,$cid,$limit=12,$offset=0)
	{					
		
		$data =  $this->db
						->limit($limit,$offset)
						->order_by('dteStart desc')
						->get_where('tb_mas_content',array('fk-tb_mas_content_types'=>$type,'fk-tb_mas_community'=>$cid,'enumEnabled'=>'enabled'))
						->result_array();
						
		return $data;
	}
	
	
	
	function get_featured_video($type,$cid)
	{
		$data =  $this->db						
						->order_by('dteStart desc')
						->get_where('tb_mas_content',array('fk-tb_mas_content_types'=>$type,'fk-tb_mas_community'=>$cid,'enumEnabled'=>'enabled'))
						->first_row();
						
		return $data;
	}
	
	function fetch_single_content($id)
	{						
		$data =  $this->db												
						->get_where('tb_mas_content',array('intID'=>$id,'enumEnabled'=>'enabled'))
						->result_array();
						
		return current($data);
	}
    
	function fetch_community_feeds($cid,$limit=10,$offset=0)
	{
        $ret = array();
		$cid = get_community_id($cid);
		$data =  $this->db
						->limit($limit,$offset)
						->order_by('dteStart desc')
						->get_where('tb_mas_content',array('fk-tb_mas_community'=>$cid,'enumEnabled'=>'enabled','fk-tb_mas_content_types !='=>5))
						->result_array();
    
					
        foreach($data as $d)
        {
             
            $d["ct-strContent"] = base64_encode($d["ct-strContent"]);
            $ret[] = $d;
        }
		return $ret;
	}
    
    function get_community_name($id)
    {
       $ret = $this->db
                 ->select('strName')
                 ->from('tb_mas_community')
                 ->where('intID',$id)
                 ->get()
                 ->result_array();
        
        $ret = current($ret);
        
        return $ret['strName'];
        
    }
    
    function get_type_name($id)
    {
       $ret = $this->db
                 ->select('strName')
                 ->from('tb_mas_content_types')
                 ->where('intID',$id)
                 ->get()
                 ->result_array();
        
        $ret = current($ret);
        
        return $ret['strName'];
        
    }


}